<?php
namespace Admin\Controller;
use Think\Controller;
class ShopController extends Controller {
	public function __construct(){
        parent::__construct();
        $this->Goods = M('Goods');
        $this->Goods_Cat = M('goods_category');
        $this->Brand = M('brand');
        $this->Brand_Cat = M('brand_cat');
    }
	/*
    title : 商品列表
	time : 2018-05-16 17:46
	username : 安林
     */
    public function Goods()
    {
    	$data = $this->Goods->select();
    	$this->data = $data;
    	$this->display();
    }
    /*
    title : 商品添加
    time : 2018-05-25 10:48
    username : 安林
     */
    public function Goods_Add()
    {
        $this->display();
    }
    /*
    title : 商品编辑
    time : 2018-05-25 10:50
    username : 安林
     */
    public function Goods_Edit()
    {
        $this->display();
    }
    /*
    title : 商品分类
    time : 2018-05-25 10:41
    username : 安林
     */
    public function Goods_Cat()
    {
        $data = $this->Goods_Cat->select();
        $this->data = $data;
        $this->display();
    }
    /*
    title : 商品分类添加
    time : 2018-05-25 10:44
    username : 安林
     */
    public function Goods_Cat_Add()
    {
        if (IS_POST) {
            $data = I('post.arr');
            if ($data['name']==false) {
                $this->error('未填写分类名称！',U('Shop/Goods_Cat_Add'));
            }else{
                $res = $this->Goods_Cat->add($data);
                if($res){
                    $this->success('分类添加成功！',U('Shop/Goods_Cat'));
                    exit;
                }else{
                    $this->error('分类添加失败(401)',U('Shop/Goods_Cat_Add'));
                    exit;
                }
            }
        }
        $this->display();
    }
    /*
    title : 编辑商品分类
    time : 2018-05-25 10:45
    username : 安林
     */
    public function Goods_Cat_Edit()
    {
        $id = I('get.id');
        $list = $this->Goods_Cat->where(array('id'=>$id))->find();
        $level = $this->Goods_Cat->field('id,name')->where(array('level'=>$data['level']-1))->select();
        if (IS_POST) {
            $data = I('post.arr');
            if ($data['name']) {
                $is_name = $this->Goods_Cat->where(array('name'=>$data['name']))->where('id!=$id')->find();
                if ($is_name) {
                    $this->error('分类名称已存在！不能重复',U('Shop/Goods_Cat_Edit?id='.$id));
                    exit;
                }else{
                    $res = $this->Goods_Cat->where(array('id'=>$id))->save($data);
                }
            }else{
                 $this->error('请填写分类名称！',U('Shop/Goods_Cat_Edit?id='.$id));
                exit;
            }
            if ($res) {
                $this->success('修改成功！',U('Shop/Goods_Cat'));
                exit;
            }else{
                $this->error('修改失败！',U('Shop/Goods_Cat_Edit?id='.$id));
                exit;
            }
        }
        $this->level = $level;
        $this->list = $list;
        $this->display();
    }
    /*
    title : 分类接口
    time : 2018-05-25 10:47
    username : 安林
     */
    public function Goods_Cat_level()
    {
        $level = I('post.level');
        $data = $this->Goods_Cat->field('id,name')->where(array('level'=>$level-1))->order('id desc')->select();
        $this->ajaxReturn($data,'JSON');
    }
    /*
    title : 分类删除
    time : 2018-05-25 15:37
    username : 安林
     */
    public function Goods_Cat_Del()
    {
        $id = I('post.id');
        $res = $this->Goods_Cat->where(array('id'=>$id))->delete();
        if ($res) {
            $data = [
                'res'   =>  200,
                'msg'   =>  '删除成功!'
            ];
        }else {
            $data = [
                'res'   =>  400,
                'msg'   =>  '删除失败(错误代码401)!'
            ];
        }
        $this->ajaxReturn($data);
    }
    /*
    title : 商品品牌
    time : 2018-05-25 16:25
    username : 安林
     */
    public function Brand()
    {
        $data = $this->Brand->select();
        $this->data = $data;
        $this->display();
    }
    /*
    title : 品牌分类
    time : 2018-05-25 16:25
    username : 安林
     */
    public function Brand_Cat()
    {
        $data = $this->Brand_Cat->select();
        $this->data = $data;
        $this->display();
    }
    /*
    title : 添加品牌分类
    time : 2018-05-25 16:35
    username : 安林
     */
    public function Brand_Cat_add()
    {
        if (IS_POST) {
            $data = I('post.arr');

        }
        $this->display();
    }
    /*
    title : 修改品牌分类
    time : 2018-05-25 16:25
    username : 安林
     */
    public function Brand_Cat_Edit()
    {
        $id = I('get.id');
        $list = $this->Brand_Cat->where(array('id'=>$id))->find();
        $this->list = $list;
        $this->display();
    }
}